<?php


class Hotel_model extends MY_Model {
	
	function Hotel_model() {
		parent::__construct();
	}
	
	
	public function getLayoutInfo() {
		
		// path location of the web site
		$data['base_url']				= $this->config->item('base_url');
		
		// create the links to translate the page
		$switch_lang				= 	anchor($this->lang->switch_uri('en'),'en')." | ";
		$switch_lang				.= 	anchor($this->lang->switch_uri('pt'),'pt');
		$data['switch_lang']		= 	$switch_lang;
		
		// load the content text data ( all data is loaded ... it doesnt look the perfect solution ... find another one :0) )
		$data['text_main']			= $this->lang->line('hotel.text.main');
		$data['text_lnk1']			= $this->lang->line('hotel.text.lnk1');
		$data['text_lnk2']			= $this->lang->line('hotel.text.lnk2');
		$data['text_lnk3']			= $this->lang->line('hotel.text.lnk3');
		
		// ... more data
		$data['website_title']		= 'Radical Booking';
		$data['title']		 		= 'Hotel';
		$data['login']				= anchor("Hotel/logout","logout")." ".$this->ion_auth->get_user()->email;
		$data['subtitle'] 			= $this->lang->line('hotel.text.subtitle');
		$data['footer'] 			= 'ï¿½ copyright Softingal';
		
		return $data;
	}
	
	
	
	
	/**
	 * function to retrieve an array with 'school_id'=>'company' of all schools 
	 * associated with the given hotel 'id'
	 * @param int $id hotel id
	 * @return array with 'school_id'=>'company'
	 */
	public function getHotelASschool($id) {
		
		$schoolNames = array();
		
		$this->db->where('hotel_id',$id);
		
		$query = $this->db->get('sh');
		
 		$result = $query->result();
		
		foreach ($result as $key => $value) {
			$schoolNames[$value->school_id] = $this->ion_auth->get_user($value->school_id)->company;
		}
		
		return $schoolNames;
	}
	
	
	/**
	 * Get the id of the school associated to the given hotel id
	 * @param int $id hotel identification
	 * @return int $id school identification or false if there is no $id to return
	 */
	public function getSchoolID($id) {
		
		$this->db->where('hotel_id',$id);
		
		$query = $this->db->get('sh');
		
		$row = $query->row();
		
		if ($query->num_rows()>0)
			return $row->school_id;
		else
			return false;
	}
	
	
	
	/**
	 * Get the 'offer_id', 'name', 'price', 'level', 'number_of_classes' and the number of available spots of
	 * all available and active offers from the school with the given 'id'    
	 * @param int $id The school identification
	 * @return array $result An array of objects with the fields (offer_id,name,price,level,number_of_classes, spot) 
	 */
	public function getAvailableOffers($id) {
		
		$now = date('Y-m-d H:i:s',time());
		
		$this->db->select('offer_id, name, price, level,(offer.maxPart - offer.part) as spots, count(*) as number_of_classes');
		$this->db->where('users_id',$id);
		$this->db->where('remainingClasses',0);
		$this->db->where('active',1);
		$this->db->from('offer');
		$this->db->join('schedule','schedule.offer_id = offer.id');
		$this->db->where('start >', $now);
		$this->db->group_by("offer_id");
		
		$query = $this->db->get();
		
		$result = $query->result(); 
		return $result;	
	}
	
	
	
	
	
	
	public function createClient($post) {
		
		$users_id = $this->ion_auth->get_user()->id;
		
		$this->db->set('users_id',$users_id);
		
		$succ = $this->db->insert('client',$post);
		
		if ($succ)
			return true;
		else 
			return false;
	}
	
	public function readClient() {
		
		$users_id = $this->ion_auth->get_user()->id;
		$this->db->select('*');
		$this->db->from('client');
		$this->db->where('users_id',$users_id);
		$query = $this->db->get();
		$rows = $query->result();
		$dataTableResult['data'] = $rows;
		return json_encode($dataTableResult);
		//return $rows;
	}	
	
	public function upadteClient() {
		
	}
	
	public function deleteClient() {
		
	}
	
	
	
	
	
	
	public function createReservation($data) {
		
		$offer_id = $data['offer_id'];
		
		// get current time (datetime)
		$now = date('Y-m-d H:i:s',time());
		$data = array_merge($data,array('date'=>$now));
		
		// create the reservation
		$succ = $this->db->insert('reservation', $data);

		if ($succ) {
			
			// get the number of participations in the offer
			$part = $this->db->select('part')->from('offer')->where('id',$offer_id)->get()->row()->part;
	
			// update by one the participant number
			$part++;
			$updt = $this->db->update('offer',array('part'=>$part),array('id'=>$offer_id));
			
			if ($updt)
				return true;
			else
				return false;
		}
		else 
			return false;
	}
	
	public function readReservation() {
		
		// only show reservation's for the current log in hotel
		$users_id = $this->ion_auth->get_user()->id;

		//$this->db->select('*');
 		$this->db->select('
 			users.id			as userId,
 			users.group_id		as groupId,
 			client.id			as clientId,
 			client.name			as clientName,
 			reservation.id		as reservationId,
 			reservation.date	as reservationDate,
 			offer.id			as offerId,
 			offer.name			as offerName
 		');

		$this->db->from('reservation');
		$this->db->join('client','client.id = reservation.client_id');
		$this->db->join('users','users.id = client.users_id');
		$this->db->join('offer','offer.id = reservation.offer_id');
		$this->db->where('users.id',$users_id);
		$query = $this->db->get();
		$rows = $query->result();
		$dataTableResult['data'] = $rows;
		
		//return $rows;
		return json_encode($dataTableResult);
	}
	
	public function updateReservation() {
		
	}
	
	public function deleteReservation() {
		
	}
	

	public function readSchedule($offerId,$userId) {
		
		// get the id of the school associated to the hotel with the given userId
 		$schoolId = $this->db->select('*')->from('sh')->where('hotel_id',$userId)->get()->row()->school_id;
 		
 		$this->db->flush_cache();
		
		$now = date('Y-m-d H:i:s',time());
		
		// users.id is a school for sure because offer is only created by school
		//$this->db->select('*');
		$this->db->select('
			users.id 			as schoolId,
			schedule.start		as start,
			schedule.end		as end,
			offer.id			as offerId
		');
		$this->db->from('schedule');
		$this->db->where('start >',$now);
		$this->db->join('offer','offer.id = schedule.offer_id');
		$this->db->join('users','users.id = offer.users_id');
		//$this->db->join('sh','sh.school_id = users.id');
		
		// return only the schedules from the school associated to the given hotel (userId)
		$this->db->where(array('users.id'=>$schoolId,'offer.id'=>$offerId));
		
		$query = $this->db->get();
		$row = $query->result();
		
		return $row;	
	}
	
	
	
	public function readOffer($offerId,$userId) {
		
		// get the id of the school associated to the hotel with the given userId
		$schoolId = $this->db->select('*')->from('sh')->where('hotel_id',$userId)->get()->row()->school_id;
			
		$this->db->flush_cache();
		
		//$now = date('Y-m-d H:i:s',time());
		
		// users.id is a school for sure because offer is only created by school
		//$this->db->select('*');
		$this->db->select('
			users.id 			as schoolId,
			offer.id			as offerId,
			offer.name			as offerName,
			offer.price			as offerPrice,
			offer.description	as offerDescription,
			offer.level			as offerLevel,
			offer.minPart		as offerMinPart,
			offer.maxPart		as offerMaxPart,
			offer.part			as offerPart
		');
		
		$this->db->from('offer');
		//$this->db->where('start >',$now);
		$this->db->join('users','users.id = offer.users_id');
		//$this->db->join('sh','sh.school_id = users.id');
		
		// return only the schedules from the school associated to the given hotel (userId)
		$this->db->where(array('users.id'=>$schoolId,'offer.id'=>$offerId));
		
		$query = $this->db->get();
		$row = $query->result();
		
		return $row;
	}
	
	
	public function tmp() {

		
	}
	
	
	
}